package cn.blog.controller.front;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.blog.controller.BaseServlet;
import cn.blog.entity.addBean.RzglArtInfo;
import cn.blog.entity.origin.Article2;
import cn.blog.entity.origin.ClassBean;
import cn.blog.service.back.Artile2Service;
import cn.blog.service.front.CompileHeadService;
import cn.blog.service.front.RizhiglService;
import cn.blog.service.front.Xiugaimima_rightService;

@WebServlet("/front/rizhigl")
public class Rizhigl extends BaseServlet {
	boolean isStr2Num(String str) {   
		try {  
			Integer.parseInt(str);  
		    return true;  
		} catch (NumberFormatException e) {  
		    return false;  
		} 
	}  
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		CompileHeadService chs = new CompileHeadService();
		// 获取用户id
		String userId = req.getSession().getAttribute("userId").toString();
		int uid = Integer.parseInt(userId);

		RizhiglService xgrs = new RizhiglService();
		// 查询所有文章类型
		List<ClassBean> cla = xgrs.classAll();
		req.setAttribute("cla", cla);
		
		String artId = req.getParameter("text");// 博客账号
		int article = 0;
		if(isStr2Num(artId)) {
			article = Integer.parseInt(artId);
		}
		String clas = req.getParameter("text");// 文章类型名称
		RizhiglService rs = new RizhiglService();
		List<RzglArtInfo> art = rs.getArticle(uid, "已发表", article, clas);
		if (art.size() == 0) {
			art = rs.getArticleAll(uid,"已发表");// 调用查询所有博客文章方法
		}
		req.setAttribute("art", art);
		
		List<RzglArtInfo> art1 = rs.getArticle(uid, "草稿", article, clas);
		if (art1.size() == 0) {
			art1 = rs.getArticleAll(uid,"草稿");// 调用查询所有博客文章方法
		}
		req.setAttribute("art1", art1);
		
		
		forward("front/rizhigl");
		
		
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 文章类型
		String classId = req.getParameter("classId");
		/*int claId = Integer.parseInt(classId);*/
		// 文章标题
		String title = req.getParameter("title");
		// 文章内容
		String artText = req.getParameter("artText");
		// 文章状态
		String state = req.getParameter("statement");
		if(state!=null) {
			if (state.equals("发表")) {
				state = "已发表";
			} else if (state.equals("存草稿")) {
				state = "草稿";
			}

		}
		// 获取系统当前时间，并转换为datetime类型
		Date time = new Date();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String t = sdf.format(time);
		java.util.Date date = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(t);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		java.sql.Date artTime = new java.sql.Date(date.getTime());

		CompileHeadService chs = new CompileHeadService();
		// 获取用户id
		String userId = req.getSession().getAttribute("userId").toString();
		int uid = Integer.parseInt(userId);

		RizhiglService xgrs = new RizhiglService();
		// 添加文章
		if(title!=null && classId!=null && artTime!=null && artText!=null && state!=null) {
			xgrs.addTitle(uid, title, Integer.parseInt(classId), artTime, 0, artText, state);
		}
		
		
		String xgartId = req.getParameter("xgartId");
		String xgartTitle = req.getParameter("xgartTitle");
		String xfartclassId = req.getParameter("xfartclassId");
		if(xgartId!=""&&xgartId!=null && xgartTitle!=""&&xgartTitle!=null && xfartclassId!=""&&xfartclassId!=null ) {
			
			xgrs.updateArt(xgartTitle,Integer.parseInt(xfartclassId),Integer.parseInt(xgartId),"已发表");
			
		}
		String caogaoxgartId = req.getParameter("caogaoxgartId");
		String caogaoxgartTitle = req.getParameter("caogaoxgartTitle");
		String caogaoxfartclassId = req.getParameter("caogaoxfartclassId");
		if(caogaoxgartId!=""&&caogaoxgartId!=null && caogaoxgartTitle!=""&&caogaoxgartTitle!=null 
				&& caogaoxfartclassId!=""&&caogaoxfartclassId!=null ) {
			
			xgrs.updateArt(caogaoxgartTitle,Integer.parseInt(caogaoxfartclassId),Integer.parseInt(caogaoxgartId),"草稿");
			
		}
		redirect("/front/rizhigl");
	}
}
